package com.xujiang.imitationcompanypj.data.model;

import android.content.Context;
import android.support.annotation.StringRes;
import android.text.TextUtils;
import com.etekcity.vesync.common.util.ISystemException;
import com.etekcity.vesync.common.util.L;
import com.etekcity.vesync.common.util.NetServerException;
import com.etekcity.vesync.common.util.SystemException;
import com.xujiang.imitationcompanypj.R;

import java.net.ConnectException;
import java.net.SocketTimeoutException;

/**
 * Created by Happy on 2015/3/25.
 */
public class ServerError {
    /**
     * error code.
     */
    public static final int ERR_SUCCEED = 0;
    /**
     * error code.
     */
    public static final int ERR_DATABASE = 1;
    /**
     * error code.
     */
    public static final int ERR_TOKEN_EXPIRED = 2;
    /**
     * error code.
     */
    public static final int ERR_ACCOUNT_NOT_EXIST = 3;
    /**
     * error code.
     */
    public static final int ERR_PASSWORD_NOT_MATCH = 4;
    /**
     * error code.
     */
    public static final int ERR_ACCOUNT_NEED = 5;
    /**
     * error code.
     */
    public static final int ERR_PASSWORD_NEED = 6;
    /**
     * error code.
     */
    public static final int ERR_ENCODE = 7;
    /**
     * error code.
     */
    public static final int ERR_DECODE = 8;

    /**
     * cancel error.
     */
    public static final int ERR_REQUEST_INVALID_PARAM = 10;
    /**
     * task has start.
     */
    public static final int ERR_CONTROLLER_OFFLINE = 11;

    public static final int ERR_CONTROLLER_FIRM_LATEST = 12;

    public static final int ERR_CONTROLLER_BUSY = 13;

    public static final int ERR_ACCOUNT_TOO_SHORT = 14;
    public static final int ERR_ACCOUNT_TOO_LONG = 15;
    public static final int ERR_ACCOUNT_ALREADY_EXIST = 16;
    public static final int ERR_PASSWORD_TOO_SHORT = 17;
    public static final int ERR_PASSWORD_TOO_LONG = 18;
    public static final int ERR_DEVICE_NOT_EXIST = 19;
    public static final int ERR_CONTROLLER_NO_RESPONSE = 20;
    public static final int ERR_SHARE_ACCOUNT_NOT_EXIST = 21;
    public static final int ERR_SHARE_PERMISSION_NOT_ALLOW = 22;
    public static final int ERR_SHARE_ACCOUNT_REPEAT = 23;

    public static final int ERROR_TIMER_NOT_FOUND = 59;
    public static final int ERROR_TIMER_ADD_BUT_NOT_SCHEDULE = 60;

    public static final int ERROR_EMAIL_NOT_EXIST = 65;
    public static final int ERR_ACCOUNT_INCLUDE_SPECIAL_CHARACTER = 66;

    public static final int ERROR_TIMER_INVALID = 69;

    // new add by 2016-8-16 11:18:37
    public static final int ERR_ACCOUNT_NOT_EMAIL = 70;
    public static final int ERR_SEND_MAIL_FAILED = 71;
    public static final int ERR_RESETPASSWORD_VERIFY_WRONG = 72;
    //new add by 2016-8-31
    public static final int FILE_TYPE_NOT_ALLOWED = 73;
    public static final int FILE_SIZE_WRONG = 74;
    public static final int FILE_SIZE_UNKOWN = 75;
    public static final int FILE_NOT_SELECTED = 76;
    public static final int FILE_UPLOAD_INIT_ERROR = 77;
    public static final int HOME_AUTH_FAILED = 78;
    public static final int HOME_DELTE_ADMIN = 79;
    public static final int FEEDBACK_CONTACT_NOT_EMAIL = 80;
    public static final int FEEDBACK_CONCAT_TOO_LONG = 81;
    public static final int ERR_PASSWORD_NOT_NUMBERS_AND_LETTERS = 82;
    //new add by 2016-9-2
    public static final int ERR_INVITE_SELF = 83;
    public static final int ERR_INVITE_MEMBER_EXISTS = 84;
    //new add by 2016-9-6
    public static final int HOMENAME_TOO_LONG = 85;

    //by 2016-11-2
    public static final int ERR_OPRATION_FAILD= 86;

    /**
     * Get error string.
     *
     * @param err Error code string.
     * @return Error String.
     */
    public static String getErrString(Context context, final int err) {
        String errStr;
        switch (err) {
            case ERR_SUCCEED:
                errStr = getString(context, R.string.ERR_SUCCEED);
                break;
            case ERR_DATABASE:
                errStr = getString(context, R.string.ERR_DATABASE);
                break;
            case ERR_TOKEN_EXPIRED:
                errStr = getString(context, R.string.ERR_TOKEN_EXPIRED);
                break;
            case ERR_ACCOUNT_NOT_EXIST:
                errStr = getString(context, R.string.ERR_ACCOUNT_NOT_EXIST);
                break;
            case ERR_PASSWORD_NOT_MATCH:
                errStr = getString(context, R.string.ERR_PASSWORD_NOT_MATCH);
                break;
            case ERR_ACCOUNT_NEED:
                errStr = getString(context, R.string.ERR_ACCOUNT_NEED);
                break;
            case ERR_PASSWORD_NEED:
                errStr = getString(context, R.string.ERR_PASSWORD_NEED);
                break;
            case ERR_ENCODE:
                errStr = getString(context, R.string.ERR_ENCODE);
                break;
            case ERR_DECODE:
                errStr = getString(context, R.string.ERR_DECODE);
                break;
            case ERR_REQUEST_INVALID_PARAM:
                errStr = getString(context, R.string.ERR_REQUEST_INVALID_PARAM);
                break;
            case ERR_CONTROLLER_OFFLINE:
                errStr = getString(context, R.string.ERR_CONTROLLER_OFFLINE);
                break;
            case ERR_CONTROLLER_FIRM_LATEST:
                errStr = getString(context, R.string.ERR_CONTROLLER_FIRM_LATEST);
                break;
            case ERR_CONTROLLER_BUSY:
                errStr = getString(context, R.string.ERR_CONTROLLER_BUSY);
                break;
            case ERR_ACCOUNT_TOO_SHORT:
                errStr = getString(context, R.string.ERR_ACCOUNT_TOO_SHORT);
                break;
            case ERR_ACCOUNT_TOO_LONG:
                errStr = getString(context, R.string.ERR_ACCOUNT_TOO_LONG);
                break;
            case ERR_ACCOUNT_ALREADY_EXIST:
                errStr = getString(context, R.string.ERR_ACCOUNT_ALREADY_EXIST);
                break;
            case ERR_PASSWORD_TOO_SHORT:
                errStr = getString(context, R.string.ERR_PASSWORD_TOO_SHORT);
                break;
            case ERR_PASSWORD_TOO_LONG:
                errStr = getString(context, R.string.ERR_PASSWORD_TOO_LONG);
                break;
            case ERR_DEVICE_NOT_EXIST:
                errStr = getString(context, R.string.ERR_DEVICE_NOT_EXIST);
                break;
            case ERR_CONTROLLER_NO_RESPONSE:
                errStr = getString(context, R.string.ERR_CONTROLLER_NO_RESPONSE);
                break;
            case ERROR_TIMER_NOT_FOUND:
                errStr = getString(context, R.string.ERROR_TIMER_NOT_FOUND);
                break;
            case ERR_SHARE_ACCOUNT_NOT_EXIST:
                errStr = getString(context, R.string.ERR_SHARE_ACCOUNT_NOT_EXIST);
                break;
            case ERR_SHARE_PERMISSION_NOT_ALLOW:
                errStr = getString(context, R.string.ERR_SHARE_PERMISSION_NOT_ALLOW);
                break;
            case ERR_SHARE_ACCOUNT_REPEAT:
                errStr = getString(context, R.string.ERR_SHARE_ACCOUNT_REPEAT);
                break;
            case ERROR_EMAIL_NOT_EXIST:
                errStr = getString(context, R.string.ERROR_EMAIL_NOT_EXIST);
                break;
            case ERR_ACCOUNT_INCLUDE_SPECIAL_CHARACTER:
                errStr = getString(context, R.string.ERR_ACCOUNT_CANNOT_HAVE_SPECIAL_CHARACTERS);
                break;
            case ERROR_TIMER_ADD_BUT_NOT_SCHEDULE:
                errStr = getString(context, R.string.ERROR_TIMER_ADD_BUT_NOT_SCHEDULE);
                break;
            case ERROR_TIMER_INVALID:
                errStr = getString(context, R.string.ERROR_TIMER_INVALID);
                break;
            //new add
            case ERR_ACCOUNT_NOT_EMAIL:
                errStr = getString(context, R.string.ERR_ACCOUNT_NOT_EMAIL);
                break;
            case ERR_SEND_MAIL_FAILED:
                errStr = getString(context, R.string.ERR_SEND_MAIL_FAILED);
                break;
            case ERR_RESETPASSWORD_VERIFY_WRONG:
                errStr = getString(context, R.string.ERR_RESETPASSWORD_VERIFY_WRONG);
                break;
            case FILE_TYPE_NOT_ALLOWED:
                errStr=getString(context,R.string.ERR_FILE_TYPE_NOT_ALLOWED);
                break;
            case FILE_SIZE_WRONG:
                errStr=getString(context,R.string.ERR_FILE_SIZE_WRONG);
                break;
            case FILE_SIZE_UNKOWN:
                errStr=getString(context,R.string.ERR_FILE_SIZE_UNKOWN);
                break;
            case FILE_NOT_SELECTED:
                errStr=getString(context,R.string.ERR_FILE_NOT_SELECTED);
                break;
            case FILE_UPLOAD_INIT_ERROR:
                errStr=getString(context,R.string.ERR_FILE_UPLOAD_INIT_ERROR);
                break;
            case HOME_AUTH_FAILED:
                errStr=getString(context,R.string.ERR_HOME_AUTH_FAILED);
                break;
            case HOME_DELTE_ADMIN:
                errStr=getString(context,R.string.ERR_HOME_DELTE_ADMIN);
                break;
            case FEEDBACK_CONTACT_NOT_EMAIL:
                errStr=getString(context,R.string.ERR_FEEDBACK_CONTACT_NOT_EMAIL);
                break;
            case FEEDBACK_CONCAT_TOO_LONG:
                errStr=getString(context,R.string.ERR_FEEDBACK_CONCAT_TOO_LONG);
                break;
            case ERR_PASSWORD_NOT_NUMBERS_AND_LETTERS:
                errStr=getString(context,R.string.ERR_ERR_PASSWORD_NOT_NUMBERS_AND_LETTERS);
                break;
            case ERR_INVITE_SELF:
                errStr=getString(context,R.string.ERR_INVITE_SELF);
                break;
            case ERR_INVITE_MEMBER_EXISTS:
                errStr=getString(context,R.string.ERR_INVITE_MEMBER_EXISTS);
                break;
            case HOMENAME_TOO_LONG:
                errStr=getString(context,R.string.HOMENAME_TOO_LONG);
                break;
            case ERR_OPRATION_FAILD:
                errStr=getString(context,R.string.ERR_OPRATION_FAILD);
                break;
            default:
                errStr = getString(context, R.string.ERR_UNKNOWN);
                break;
        }
        return errStr;
    }

    private static String getString(Context context, @StringRes final int err) {
        return context.getString(err);
    }

    public static String getServerError(Context context, SystemException e) {
        String message = e.getMessage();
        if (!TextUtils.isEmpty(message)) {
            if (message.equals(ISystemException.DATA_ERROR)) {
                return context.getString(R.string.err_data_exceptional);
            }
        }
        return getServerError(context,e.getCause());
    }

    public static String getServerError(Context context, Throwable e) {
        if (e instanceof NetServerException) {
            NetServerException exception = (NetServerException) e;
            Object errorBody = exception.getErrorBody();
            if (errorBody instanceof String) {
                return exception.getErrorBody();
            } else if (errorBody instanceof ServerError) {
                ServerError error = (ServerError) errorBody;
                return error.getMsg();
            }
        } else {
            if (e instanceof SocketTimeoutException) {
                return getString(context, R.string.ERROR_NETWORK_TIMEOUT);
            }else if (e instanceof ConnectException){
                return getString(context, R.string.ERR_DEVICE_CAN_NOT_CONNECT_TO_SERVER);
            }
        }
        return getString(context, R.string.ERR_UNKNOWN);
    }

    public static void error(String tag, Context context, SystemException e) {
        error(tag, context, e.getCause());
    }

    public static void error(String tag, Context context, Throwable e) {
        String serverError = getServerError(context, e);
        L.getToast().e(tag, context, serverError, e);
    }

    /**
     * construct.
     *
     * @param code code
     * @param msg  message
     */
    public ServerError(int code, String msg) {
        mCode = code;
        mMsg = msg;
    }

    /**
     * construct.
     *
     * @param code code
     * @param msg  message
     */
    public ServerError(String code, String msg) {
        mCode = Integer.parseInt(code);
        mMsg = msg;
    }

    /**
     * code.
     */
    private final int mCode;
    /**
     * message.
     */
    private final String mMsg;

    /**
     * get code.
     *
     * @return code
     */
    public int getCode() {
        return mCode;
    }

    /**
     * get message.
     *
     * @return message
     */
    public String getMsg() {
        return mMsg;
    }

    @Override public String toString() {
        return "ServerError{" +
               "mCode=" + mCode +
               ", mMsg='" + mMsg + '\'' +
               '}';
    }
}
